{extend name='table'}

{block name="button"}
<!--{if isset($super) and $super}-->
<a class="layui-btn layui-btn-sm layui-btn-primary" data-queue="{:url('admin/api.plugs/optimize')}">优化数据库</a>
<!--{if $iswin}-->
<button data-load='{:url("admin/api.queue/start")}' class='layui-btn layui-btn-sm layui-btn-primary'>开启后台服务</button>
<button data-load='{:url("admin/api.queue/stop")}' class='layui-btn layui-btn-sm layui-btn-primary'>关闭后台服务</button>
<!--{/if}-->
<!--{/if}-->

<!--{if auth("clean")}-->
<button data-queue='{:url("clean")}' class='layui-btn layui-btn-sm layui-btn-primary'>定时清理数据</button>
<!--{/if}-->

<!--{if auth("remove")}-->
<button data-action='{:url("remove")}' data-rule="id#{id}" data-table-id="QueueData" data-confirm="确定批量删除记录吗？" class='layui-btn layui-btn-sm layui-btn-primary'>批量删除任务</button>
<!--{/if}-->
{/block}

{block name="content"}
<div class="think-box-shadow">
    <div class="layui-row layui-col-space20 portal-block-container notselect">
        <div class="layui-col-sm6 layui-col-md6 layui-col-lg3">
            <div class="portal-block-item nowrap" style="background:linear-gradient(-125deg,#57bdbf,#2f9de2)">
                <div class="font-w7 font-s16">等待处理</div>
                <div>{$total.pre|default=0}</div>
                <div>待处理的任务数量</div>
            </div>
            <i class="portal-block-icon layui-icon layui-icon-star"></i>
        </div>
        <div class="layui-col-sm6 layui-col-md6 layui-col-lg3">
            <div class="portal-block-item nowrap" style="background:linear-gradient(-125deg,#ff7d7d,#fb2c95)">
                <div class="font-w7 font-s16">正在处理</div>
                <div>{$total.dos|default=0}</div>
                <div>处理中的任务数量</div>
            </div>
            <i class="portal-block-icon layui-icon layui-icon-log"></i>
        </div>
        <div class="layui-col-sm6 layui-col-md6 layui-col-lg3">
            <div class="portal-block-item nowrap" style="background:linear-gradient(-113deg,#c543d8,#925cc3)">
                <div class="font-w7 font-s16">处理完成</div>
                <div>{$total.oks|default=0}</div>
                <div>处理完成的任务数量</div>
            </div>
            <i class="portal-block-icon layui-icon layui-icon-release"></i>
        </div>
        <div class="layui-col-sm6 layui-col-md6 layui-col-lg3">
            <div class="portal-block-item nowrap" style="background:linear-gradient(-141deg,#ecca1b,#f39526)">
                <div class="font-w7 font-s16">处理失败</div>
                <div>{$total.ers|default=0}</div>
                <div>处理失败的任务数量</div>
            </div>
            <i class="portal-block-icon layui-icon layui-icon-engine"></i>
        </div>
    </div>

    {include file='queue/index_search'}
    <table id="QueueData" data-url="{:sysuri()}" data-target-search="form.form-search"></table>
</div>
{/block}

{block name='script'}
<script>
    $(function () {
        $('#QueueData').layTable({
            even: true,
            sort: {field: 'loops_time desc,code', type: 'desc'},
            cols: [[
                {checkbox: true, fixed: 'left'},
                {field: 'code', title: '任务编号', width: 140, sort: true},
                {field: 'title', title: '任务名称', minWidth: 100},
                {field: 'command', title: '任务指令', minWidth: 100},
                {
                    field: 'exec_time', title: '计划时间', minWidth: 245, templet: function (d) {
                        d.exec_time = d.exec_time || 0, d.loops_time = d.loops_time || 0;
                        if (d.loops_time > 0) {
                            return d.exec_time + ' ( 每 <b class="color-blue">' + d.loops_time + '</b> 秒 ) ';
                        } else {
                            return d.exec_time + ' <span class="color-desc">( 单次任务 )</span> ';
                        }
                    }
                },
                {
                    field: 'loops_time', title: '执行时间', minWidth: 175, templet: function (d) {
                        d.enter_time = d.enter_time || '', d.outer_time = d.outer_time || '0.0000';
                        if (d.enter_time.length > 12) {
                            return d.enter_time.substr(12) + '<span class="color-desc"> ( 耗时 ' + d.outer_time + ' )</span>';
                        } else {
                            return '<span class="color-desc">任务未执行</span>'
                        }
                    }
                },
                {field: 'attempts', title: '执行次数', minWidth: 95, align: 'center', sort: true, templet: "<div>{{d.attempts||0}}</div>"},
                {field: 'exec_desc', title: '执行结果', minWidth: 180},
                {field: 'create_at', title: '创建时间', align: 'center', minWidth: 170},
                {toolbar: '#toolbar', title: '操作面板', align: 'center', width: 260, fixed: 'right',}
            ]]
        });
    });
</script>

<script type="text/html" id="toolbar">
    {{# if(d.loops_time>0){ }}
    <span class="layui-badge think-bg-blue">循</span>
    {{# }else{ }}
    <span class="layui-badge think-bg-red">次</span>
    {{# } }}

    {{# if(d.rscript===1){ }}
    <span class="layui-badge layui-bg-green">复</span>
    {{# }else{ }}
    <span class="layui-badge think-bg-violet">单</span>
    {{# } }}

    {{# if(d.status===1){ }}
    <span class="layui-badge layui-bg-black">等待处理</span>
    <span class="layui-badge think-bg-gray"><i class="layui-icon font-s12">&#xe669;</i></span>
    {{# }else if(d.status===2){ }}
    <span class="layui-badge layui-bg-green">正在处理</span>
    <!--{if auth('redo')}-->
    <span class="layui-badge think-bg-gray"><i class="layui-icon font-s12">&#xe669;</i></span>
    <!--{/if}-->
    {{# }else if(d.status===3){ }}
    <span class="layui-badge layui-bg-blue">处理完成</span>
    <!--{if auth('redo')}-->
    <a class="layui-badge layui-bg-green" data-confirm="确定要重置该任务吗？" data-queue="{:url('redo')}?code={{d.code}}">
        <i class="layui-icon font-s12">&#xe669;</i>
    </a>
    <!--{/if}-->
    {{# }else if(d.status===4){ }}
    <span class="layui-badge layui-bg-red">处理失败</span>
    <!--{if auth('redo')}-->
    <a class="layui-badge layui-bg-green" data-confirm="确定要重置该任务吗？" data-queue="{:url('redo')}?code={{d.code}}">
        <i class="layui-icon font-s12">&#xe669;</i>
    </a>
    <!--{/if}-->
    {{# } }}

    <!--{if auth('remove')}-->
    <a class='layui-badge layui-bg-red' data-confirm="确定要删除该任务吗？" data-action='{:url("remove")}' data-value="id#{{d.id}}">
        <i class="layui-icon font-s12">&#xe640;</i>
    </a>
    <!--{/if}-->

    <a class='layui-badge layui-bg-orange margin-0' onclick="$.loadQueue('{{d.code}}',false,this)">
        <i class="layui-icon font-s12">&#xe705;</i>
    </a>
</script>
{/block}
